﻿using In66.Net.Repository.IRepositories.ISqlSugar;

namespace Usr.Api.Applications.BloomFilters
{
    public class AccountBloomFilter : AbstractBloomFilter
    {
        private readonly IServiceProvider _services;

        public AccountBloomFilter(IRedisProvider redisProvider
            , IDistributedLocker distributedLocker
            , IServiceProvider services)
            : base(redisProvider, distributedLocker)
           => _services = services;

        public override string Name => CachingConsts.BloomfilterOfAccountsKey;

        public override double ErrorRate => 0.001;

        public override int Capacity => 10000000;

        public override async Task InitAsync()
        {
            var exists = await ExistsBloomFilterAsync();
            if (!exists)
            {
                using var scope = _services.CreateScope();
                var repository = scope.ServiceProvider.GetRequiredService<ISugarBaseRepository<Models.Entities.User>>();
                var values = repository.QueryListByClause(p => true)
                                             .Select(x => x.Account)
                                             .ToList();
                await InitAsync(values);
            }
        }
    }
}
